package com.monkeygroup.mygoplus.ecchat.ui.contact;

import android.content.Intent;
import android.view.View;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.monkeygroup.mygoplus.MyGoApplication;
import com.monkeygroup.mygoplus.R;
import com.monkeygroup.mygoplus.base.BaseActivity;
import com.monkeygroup.mygoplus.common.utils.VilidateUtil;
import com.monkeygroup.mygoplus.ecchat.common.utils.ToastUtil;
import com.monkeygroup.mygoplus.ecchat.ui.contact.adapter.ChooseMemberAdatper;
import com.monkeygroup.mygoplus.ecchat.ui.contact.entity.ContactMemberDTO;
import com.monkeygroup.mygoplus.http.HttpModel;
import com.monkeygroup.mygoplus.http.RequestUrl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2017/5/26.
 */
public class InviteContactActivity extends BaseActivity implements View.OnClickListener {
    private ListView contact_choose_list_view;
    private TextView btn_invite_confirm;
    private List<ContactMemberDTO> chooseFriendList = new ArrayList<>();
    private List<ContactMemberDTO> choosedList = new ArrayList<>();
    private ChooseMemberAdatper adatper;
    private final String TAG = "InviteContactActivity";

    @Override
    public void setContentView() {
        setContentView(R.layout.activity_search_contact);
    }

    @Override
    public void initView() {
        contact_choose_list_view = (ListView) findViewById(R.id.contact_choose_list_view);
        btn_invite_confirm = (TextView) findViewById(R.id.btn_invite_confirm);
        btn_invite_confirm.setText("確定");
    }

    @Override
    public void setListener() {
        findViewById(R.id.layout_back).setOnClickListener(this);
        findViewById(R.id.btn_invite_confirm).setOnClickListener(this);

    }

    @Override
    public void doEvent() {
        initAdapter();

        if (MyGoApplication.getContacts() != null && MyGoApplication.getContacts().size() > 0) {
            chooseFriendList.clear();
            chooseFriendList.addAll(MyGoApplication.getContacts().values());
            adatper.notifyDataSetChanged();
        } else {
            doAllContact();
        }
    }

    private void initAdapter() {
        adatper = new ChooseMemberAdatper(InviteContactActivity.this, chooseFriendList, new ChooseMemberAdatper.OnChooseItemClickListener() {
            @Override
            public void onChooseClick(int position, boolean flag) {
                refreshChooseData(position, flag);
            }
        });
        contact_choose_list_view.setAdapter(adatper);
    }

    private void refreshChooseData(int position, boolean flag) {
        if (flag) {
            for (int i = 0; i < choosedList.size(); i++) {
                if (choosedList.get(i).getId().equals(chooseFriendList.get(position).getId())) {
                    return;
                }
            }
            choosedList.add(chooseFriendList.get(position));
        } else {
            for (int i = 0; i < choosedList.size(); i++) {
                if (choosedList.get(i).getId().equals(chooseFriendList.get(position).getId())) {
                    choosedList.remove(i);
                    break;
                }
            }
        }
        if (choosedList.size() > 0) {
            btn_invite_confirm.setText("確定（" + choosedList.size() + "）");
        } else {
            btn_invite_confirm.setText("確定");
        }
    }

    private void setFriendsData(List<Map<String, String>> list) {
        List<ContactMemberDTO> friendList = new ArrayList<>();
        for (Map<String, String> item : list) {
            ContactMemberDTO memberDTO = new ContactMemberDTO();
            if (item.get("username") != null && !item.get("username").equals("")) {
                memberDTO.setName(item.get("username"));
            } else if (item.get("userID") != null && !item.get("userID").equals("")) {
                memberDTO.setName(item.get("userID"));
            } else {
                memberDTO.setName(item.get("mobile"));
            }

            memberDTO.setId(item.get("mobile"));
            memberDTO.setHeadUrl(item.get("headIcon"));
            memberDTO.setStatus(item.get("status"));
            memberDTO.setEmail(item.get("email"));
            memberDTO.setIsOffice(item.get("isOffice"));
            memberDTO.setNotification(item.get("notification"));
            memberDTO.setRemark(item.get("remark"));
            friendList.add(memberDTO);
        }

        chooseFriendList.clear();
        chooseFriendList.addAll(friendList);
        adatper.notifyDataSetChanged();

        Map<String, ContactMemberDTO> friends = new HashMap<>();
        for (ContactMemberDTO memberDTO : friendList) {
            friends.put(memberDTO.getId(), memberDTO);
        }
        MyGoApplication.setContacts(friends);
    }

    private void doAllContact() {
        if (MyGoApplication.myECUserid.equals(""))
            return;

        String requestUrl = RequestUrl.baseUrl + RequestUrl.requestAllContact;
        HttpModel httpModel = new HttpModel(requestUrl, getParams(), new HttpModel.SimpleOnHttpRequestListener() {
            @Override
            public void onRequestFinished(int resultCode, String resultMsg, Object data) {
                if (VilidateUtil.isSuccess(resultCode)) {
                    Map<String, Object> map = (Map) data;
                    List<Map<String, String>> list = (ArrayList) map.get("friends");
                    setFriendsData(list);

                } else {
                    Toast.makeText(InviteContactActivity.this, resultMsg, Toast.LENGTH_SHORT).show();
                }
            }
        }, true);

        httpModel.startRequest();
    }

    private Map<String, String> getParams() {
        Map<String, String> params = new HashMap<>();
        params.put("mobile", MyGoApplication.myECUserid);

        return params;
    }

    private ArrayList<String> getChooseList() {
        ArrayList<String> list = new ArrayList<>();
        for (ContactMemberDTO memberDTO : choosedList) {
            list.add(memberDTO.getId());
        }

        return list;
    }

    @Override
    public void onClick(View v) {
        Intent intent = new Intent();
        switch (v.getId()) {
            case R.id.layout_back:
                finish();
                break;
            case R.id.btn_invite_confirm:
                if (choosedList.size() < 1) {
                    ToastUtil.showMessage("請至少選擇1位好友！");
                    return;
                }
                intent.putStringArrayListExtra("list", getChooseList());
                setResult(RESULT_OK, intent);
                InviteContactActivity.this.finish();
                break;
            default:
                break;
        }
    }
}